package com.skh.hdevboard.slice;

import com.kaihong.template.utils.Logger;
import com.skh.hdevboard.ElementUtil;
import com.skh.hdevboard.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.*;
import ohos.agp.components.webengine.Navigator;
import ohos.agp.components.webengine.ResourceRequest;
import ohos.agp.components.webengine.WebAgent;
import ohos.agp.components.webengine.WebView;
import ohos.agp.text.Layout;
import ohos.agp.utils.TextTool;
import ohos.utils.net.Uri;
import ohos.utils.zson.ZSONObject;


public class WebAbilitySlice extends AbilitySlice {
    Navigator navigator;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_web);
        Text title = (Text) findComponentById(ResourceTable.Id_tvTitle);
        Image back = (Image) findComponentById(ResourceTable.Id_back);
        WebView webView = (WebView) findComponentById(ResourceTable.Id_webview);
        webView.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        webView.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
        String url = "https://harmonyos.51cto.com/column/59"; // EXAMPLE_URL由开发者自定义
        String params = intent.getStringParam("params");
        if (!TextTool.isNullOrEmpty(params)) {
            ZSONObject dataObject = ZSONObject.stringToZSON(params);
//            url = dataObject.getString("url");
            String titleName = dataObject.getString("title");
            Logger.i("WebAbilitySlice params url:" + url);
            title.setText(titleName);
        }

        webView.getWebConfig().setJavaScriptPermit(true);  // 如果网页需要使用JavaScript，增加此行；如何使用JavaScript下文有详细介绍
        webView.setWebAgent(new ExampleWebAgent());
        webView.load(url);
        navigator = webView.getNavigator();

        back.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });
    }

    private static class ExampleWebAgent extends WebAgent {
        public static final String EXAMPLE_URL = "...";

        @Override
        public boolean isNeedLoadUrl(WebView webview, ResourceRequest request) {
            if (request == null || request.getRequestUrl() == null) {
                return false;
            }
            Uri uri = request.getRequestUrl();
            // EXAMPLE_URL由开发者自定义
            if (uri.getDecodedHost().equals(EXAMPLE_URL)) {
                // 增加开发者自定义逻辑
                return false;
            } else {
                return super.isNeedLoadUrl(webview, request);
            }
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackPressed() {
        if (navigator != null && navigator.canGoBack()) {
            navigator.goBack();
        } else {

            super.onBackPressed();
        }
    }
}
